package com.yhj.reggie.service.impl;

import com.yhj.reggie.entity.ShoppingCart;
import com.yhj.reggie.mapper.ShoppingCartMapper;
import com.yhj.reggie.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * 加入购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart addCart(ShoppingCart shoppingCart) {
        //1,判读是否有重复对象
        ShoppingCart dbshoppingCart = shoppingCartMapper.findBySC(shoppingCart);
        if (dbshoppingCart!=null){
            dbshoppingCart.setNumber(dbshoppingCart.getNumber()+1);

            shoppingCartMapper.updateNumber(dbshoppingCart);

        }else {
            dbshoppingCart=shoppingCart;
            dbshoppingCart.setCreateTime(LocalDateTime.now());

            dbshoppingCart.setNumber(1);

            shoppingCartMapper.save(dbshoppingCart);
        }


        return dbshoppingCart;
    }

    /**
     * 显示购物车清单
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> findAllByUser(Long userId) {
        return shoppingCartMapper.findAllByUser(userId);
    }


    /**
     * 清空购物车
     *
     * @param userId
     */
    @Override
    public void deleteAll(Long userId) {
        shoppingCartMapper.deleteAll(userId);
    }
}
